package cn.hs.ph.servlet;

import cn.hs.ph.dao.UserDao;
import cn.hs.ph.dao.impl.UserDaoImpl;
import cn.hs.ph.entity.User;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class Userservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码方式
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        UserDao userDao=new UserDaoImpl();
        User user = userDao.selectByUsernameAndPwd(username, pwd);
        Result res=null;
        if (user==null){
             res = Result.error("账号或密码错误！");
        }else{
            res = Result.success("登录成功！");
        }
        resp.getWriter().write(JSON.toJSONString(res));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
